package com.sctvcloud.dongqu.ui.activities;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.canyinghao.canrefresh.other.CanRecyclerViewHeaderFooter;
import com.jiongbull.jlog.JLog;
import com.luck.picture.lib.PictureExternalPreviewActivity;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.ui.util.ViewUtils;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.ui.widget.TextViewExpandableAnimation;
import com.ruihang.generalibrary.utils.DateUtils;
import com.ruihang.generalibrary.utils.DensityUtil;
import com.ruihang.generalibrary.utils.ListUtils;
import com.ruihang.generalibrary.utils.UrlUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.application.Cache;
import com.sctvcloud.dongqu.application.Constances;
import com.sctvcloud.dongqu.base.BaseDetailActivity;
import com.sctvcloud.dongqu.beans.ADynamicsItem;
import com.sctvcloud.dongqu.beans.ContentImage;
import com.sctvcloud.dongqu.beans.DoUpEvent;
import com.sctvcloud.dongqu.beans.FComment;
import com.sctvcloud.dongqu.beans.FRebellion;
import com.sctvcloud.dongqu.beans.ImageUr;
import com.sctvcloud.dongqu.beans.SingleResult;
import com.sctvcloud.dongqu.http.AbsNetCallBack;
import com.sctvcloud.dongqu.http.NetUtils;
import com.sctvcloud.dongqu.http.ParamsEditor;
import com.sctvcloud.dongqu.ui.adapter.AnchorHomePageAdapter;
import com.sctvcloud.dongqu.ui.adapter.CommentDetailAdapter;
import com.sctvcloud.dongqu.ui.adapter.PicAdapter;
import com.sctvcloud.dongqu.ui.adapter.holder.AnchorHomeDynamicsHolder;
import com.sctvcloud.dongqu.ui.util.RBaseItemDecoration;
import com.sctvcloud.dongqu.ui.utils.GlideCircleTransform;
import com.sctvcloud.dongqu.ui.utils.GlideUtil;
import com.sctvcloud.dongqu.ui.utils.ItemImagTarget;
import com.sctvcloud.dongqu.ui.utils.SkipUtil;
import com.sctvcloud.dongqu.ui.utils.TitleUtils;
import com.sctvcloud.dongqu.utils.UserManager;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 评论详情(爆料的评论)
 */
public class BaoCommentDetailActivity extends BaseDetailActivity implements CanRefreshLayout.OnRefreshListener
        , CanRecyclerViewHeaderFooter.OnLoadMoreListener {

    @BindView(R.id.comment_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView comment_list;
    @BindView(R.id.comment_footer)
    protected CanRecyclerViewHeaderFooter footer;
    @BindView(R.id.comment_foot_pb)
    protected ProgressBar pb;
    @BindView(R.id.comment_foot_loadmore)
    protected View allLoaded;
    @BindView(R.id.comment_foot_lay_normal)
    protected ViewGroup layFootNormal;
    @BindView(R.id.comment_foot_lay_empty)
    protected View layFootEmpty;
    @BindView(R.id.item_anchor_home_dynamics_img)
    protected ImageView img;
    @BindView(R.id.item_anchor_dynamics_home_name)
    protected CustomFontTextView name;
    @BindView(R.id.item_anchor_dynamics_home_time)
    protected CustomFontTextView time;
    //    @BindView(R.id.item_anchor_dynamics_home_lable)
//    protected CustomFontTextView lable;
    @BindView(R.id.item_anchor_dynamics_home_page_intro)
    protected TextViewExpandableAnimation intro;
    @BindView(R.id.item_anchor_dynamic_home_pic)
    protected RecyclerView pic_list;

    @BindView(R.id.item_anchor_home_dynamics_video_rl)
    protected ViewGroup video_rl;
    @BindView(R.id.item_anchor_home_dynamics_video_coverImg)
    protected ImageView coverImage;
    @BindView(R.id.item_achor_single_temp)
    protected View singleTemp;
    @BindView(R.id.item_anchor_home_dynamics_video_play)
    protected ImageView play;
    @BindView(R.id.item_anchor_dynamics_home_page_ll_four)
    protected ViewGroup four_ll;
    @BindView(R.id.item_anchor_dynamics_home_page_img1)
    protected CustomEXImageView img1;
    @BindView(R.id.item_anchor_dynamics_home_page_img2)
    protected CustomEXImageView img2;
    @BindView(R.id.item_anchor_dynamics_home_page_img3)
    protected CustomEXImageView img3;
    @BindView(R.id.item_anchor_dynamics_home_page_img4)
    protected CustomEXImageView img4;
    @BindView(R.id.item_anchor_dynamics_home_page_img5)
    protected CustomEXImageView img5;
    @BindView(R.id.item_anchor_dynamics_home_page_img6)
    protected CustomEXImageView img6;
    @BindView(R.id.item_anchor_dynamics_home_page_rl)
    protected ViewGroup rl_type;

    @BindView(R.id.tv_anchor_up)
    protected CustomFontTextView tvUp;
    @BindView(R.id.tv_anchor_comment)
    protected CustomFontTextView tvComment;
    @BindView(R.id.item_anchor_dynamics_home_page_comment)
    protected LinearLayout llComment;
    private PicAdapter picAdapter;
    @BindView(R.id.tv_comment_count)
    protected CustomFontTextView tv_comment_count;
    @BindView(R.id.bao_title)
    protected CustomFontTextView bao_title;
    @BindView(R.id.rebllion_commit_address_title)
    protected CustomFontTextView address_cv;

    private LinearLayoutManager manager;
    private CommentDetailAdapter adapter;
    private FRebellion dynamicsItem;
    //    private String id;
    private int capacity = 20;//请求数据条数
    private int indexNumber = 0;
    private int requestType = 4;// 1代表新闻评论  2专题评论 3主播评论 4报料评论
    private int commentType = 4;   //0新闻评论  1专题评论 2 直播评论 4报料评论

    private Handler handler;
    private int position;
    private ArrayList<ContentImage> contentImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this);
        this.setTypeName(2);//日志统计 主播圈类型
        initView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_bao_comment_detail);
        butterUnbinder = ButterKnife.bind(this);
    }

    @Override
    public void onLoadMore() {
        if (refreshLayout.isRefreshing()) {
            footer.loadMoreComplete();
        } else {
            indexNumber += 20;
            getCommentNews(dynamicsItem.getInfoId(), requestType, capacity, indexNumber);
        }
    }

    @Override
    public void onChildAttached(View v) {
        if (adapter.getItemCount() == 1 && v != null && layFootEmpty != null) {
//            JLog.e("bbvv  rfh=" + refreshLayout.getHeight() + "   vh=" + v.getHeight() + "  eh=" + layFootEmpty.getHeight());
            int h = refreshLayout.getHeight() - v.getHeight() - DensityUtil.dip2px(this, 30);
            if (h > layFootEmpty.getHeight()) {
                ViewGroup.LayoutParams lp = layFootEmpty.getLayoutParams();
                lp.height = h;
                layFootEmpty.setLayoutParams(lp);
            }
        }
    }

    @Override
    public void onRefresh() {
        capacity = 20;
        indexNumber = 0;
        getCommentNews(dynamicsItem.getInfoId(), requestType, capacity, indexNumber);
    }
    /**
     * 获取全部数据
     *
     * @param ca
     */
    private void requestAll(int ca) {
        capacity = ca;
        indexNumber = 0;
        getCommentNews(dynamicsItem.getInfoId(), requestType, capacity, indexNumber);
    }
    private void initView() {
        handler = new Handler();
        refreshLayout.setOnRefreshListener(this);
        manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        comment_list.setItemAnimator(new DefaultItemAnimator());
        comment_list.setLayoutManager(manager);
        comment_list.addItemDecoration(new RBaseItemDecoration(DensityUtil.dip2px(this, 1),
                ContextCompat.getColor(this, R.color.colorItemListDivider)));
        Intent intent = getIntent();
        if (intent == null) {
            toast(R.string.data_wrong);
            finish();
            return;
        }
        dynamicsItem = (FRebellion) intent.getSerializableExtra(EX_DATA);
        initTop(dynamicsItem);
        requestType = getIntent().getIntExtra(EX_TYPE_NEWS, 4);
        commentType = getIntent().getIntExtra(EX_COMMENT_TYPE, 4);
        position =  getIntent().getIntExtra(DO_UP_POS, -1);

        footer.attachTo(comment_list, false);
        footer.setLoadMoreListener(this);
        footer.setVisibility(View.GONE);
        refreshLayout.autoRefresh();

    }

    @OnClick(value = {R.id.comment_write_comment})
    public void commentClick(View view) {
        showCommentDialog(dynamicsItem.getInfoId(), "", commentType);  //0代表新闻评论
    }

    @Override
    protected void onCommentGetListSuc(int count, List<FComment> fProgrammes) {
        super.onCommentGetListSuc(count, fProgrammes);
        if (indexNumber >= count || count < capacity) {
            footer.setLoadEnable(false);
            pb.setVisibility(View.GONE);
            allLoaded.setVisibility(View.VISIBLE);
            List<FComment> list =  dataHandle(fProgrammes);
            initInfo(list);
            tv_comment_count.setText(getResources().getString(R.string.comments_text)+" "+count);

        } else {
            requestAll(count);

        }

    }


    @Override
    protected void onCommentNetFinish(int mode, boolean isNetSuc) {
        if (mode == Constances.MODE_COMMENT_GET_NEW) {
            onNetFinish();
        }
        super.onCommentNetFinish(mode, isNetSuc);
    }

    @Override
    protected void onCommentSendSuc(int mode) {
        super.onCommentSendSuc(mode);
        onRefresh();
    }

    private synchronized void onNetFinish() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.refreshComplete();
        } else {
            footer.loadMoreComplete();
        }

    }
    /**
     * 处理评论数据
     *
     * @param datas
     * @return
     */
    private List<FComment> dataHandle(List<FComment> datas) {
        List<FComment> comments = new ArrayList<>();
        List<FComment> answers = new ArrayList<>();
        if (null != datas) {
            FComment comment;
            for (int i = 0; i < datas.size(); i++) {
                comment = datas.get(i);
                if (TextUtils.isEmpty(comment.getAnswerTo())) {
                //    if (comments.size() < 5) {
                        comments.add(comment);
                //    }
                } else {
                    answers.add(comment);
                }
            }

            for (int j = 0; j < answers.size(); j++) {
                FComment co = answers.get(j);
                for (int k = 0; k < comments.size(); k++) {
                    if(!TextUtils.isEmpty(co.getAnswerToCommentId())&&co.getAnswerToCommentId().equalsIgnoreCase(comments.get(k).getCommentId())){
                        comments.add(k + 1, co);
                        break;
                    }
                }
            }
        }
        return comments;

    }

    private List<FComment> getTargetAnswerList(FComment target,List<FComment> list){
        List<FComment> answers = new ArrayList<>();
            for (int k = 0; k < list.size(); k++) {
                if(!TextUtils.isEmpty(list.get(k).getAnswerToCommentId())&&list.get(k).getAnswerToCommentId().equalsIgnoreCase(target.getCommentId())){
                    answers.add(list.get(k));
                }
            }

        return answers;

    }


    private void initInfo(List<FComment> datas) {
            if (ListUtils.isListValued(datas)) {
                llComment.setVisibility(View.VISIBLE);
                llComment.removeAllViews();
                LayoutInflater inflater = LayoutInflater.from(BaoCommentDetailActivity.this);

                    for(final FComment ff : datas){
                        if(!TextUtils.isEmpty(ff.getAnswerToCommentId())){
                            continue;
                        }
                        List<FComment> answers = getTargetAnswerList(ff,datas);
                        LinearLayout root = (LinearLayout) inflater.inflate(R.layout.item_author_comment_topic, null);
                        if(null != answers && answers.size()>0){
                            root.findViewById(R.id.dv_item).setVisibility(View.GONE);
                        }else{
                            root.findViewById(R.id.dv_item).setVisibility(View.VISIBLE);
                        }
                        CustomEXImageView avatar = (CustomEXImageView)root.findViewById(R.id.item_rebllion_avatar);
                        if (!TextUtils.isEmpty(ff.getAvatar())) {
                            //GlideUtil.loadAvatorDef(context, fRebellion.getUserAvator(), avatar);
                            GlideUtil.getGlid(BaoCommentDetailActivity.this,ff.getAvatar()).error(R.mipmap.icon_circle_avatar)
                                    .placeholder(R.mipmap.icon_circle_avatar).transform(new GlideCircleTransform(BaoCommentDetailActivity.this))
                                    .into(avatar);
                        } else {
                            avatar.setImageResource(R.mipmap.icon_circle_avatar);
                        }

                        ((CustomFontTextView)root.findViewById(R.id.item_rebllion_username)).setText(TextUtils.isEmpty(ff.getNickName()) ? "" : ff.getNickName());
                        ((TextViewExpandableAnimation)root.findViewById(R.id.item_rebllion_pic_title)).setText(ff.getComment());
                        //time.setText(TextUtils.isEmpty(fRebellion.getTime()) ? context.getString(R.string.default_time) : fRebellion.getTime());
                        ((CustomFontTextView)root.findViewById(R.id.item_rebllion_time)).setText(TextUtils.isEmpty(ff.getPubTime()) ? "" :DateUtils.timeAgo(DateUtils.getDataDate(ff)));
                        (root.findViewById(R.id.iv_comment)).setVisibility(View.VISIBLE);
                        (root.findViewById(R.id.iv_comment)).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                showCommentDialog(dynamicsItem.getInfoId(), ff.getCommentId(), commentType);
                            }
                        });
                        llComment.addView(root);
                        if(null != answers && answers.size()>0){

                            for(FComment ff_ans: answers){

                                LinearLayout root_answer = (LinearLayout) inflater.inflate(R.layout.item_topic_detail_comment_answer_to, null);

                                CustomEXImageView avatar_answer = (CustomEXImageView)root_answer.findViewById(R.id.answer_item_topic_detail_comment_icon_answer_to);

                                if (!TextUtils.isEmpty(ff.getAvatar())) {
                                    //GlideUtil.loadAvatorDef(context, fRebellion.getUserAvator(), avatar);
                                    GlideUtil.getGlid(BaoCommentDetailActivity.this,ff_ans.getAvatar()).error(R.mipmap.icon_circle_avatar)
                                            .placeholder(R.mipmap.icon_circle_avatar).transform(new GlideCircleTransform(BaoCommentDetailActivity.this))
                                            .into(avatar_answer);
                                } else {
                                    avatar_answer.setImageResource(R.mipmap.icon_circle_avatar);
                                }

                               // ((CustomFontTextView)root_answer.findViewById(R.id.answer_item_topic_detail_comment_username_answer_to)).setText(TextUtils.isEmpty(ff_ans.getNickName()) ? "" : ff_ans.getNickName());

                                ((TextViewExpandableAnimation)root_answer.findViewById(R.id.answer_item_topic_detail_comment_username_answer_to)).setTextHtml("<font color=\"#F75C43\">" + (TextUtils.isEmpty(ff_ans.getNickName()) ? "" : TextUtils.isEmpty(ff_ans.getNickName()) ? "" : ff_ans.getNickName()) + "</font>"+"回复"+
                                        "<font color=\"#F75C43\">" +(TextUtils.isEmpty(ff_ans.getAnswerTo()) ? "" : ff_ans.getAnswerTo()+ "</font>" ),true);

                                ((TextViewExpandableAnimation)root_answer.findViewById(R.id.answer_item_topic_detail_comment_comment_answer_to)).setText(ff_ans.getComment());
                                ((CustomFontTextView)root_answer.findViewById(R.id.answer_item_topic_detail_comment_time_answer_to)).setText(TextUtils.isEmpty(ff_ans.getPubTime()) ? "" :DateUtils.timeAgo(DateUtils.getDataDate(ff_ans)));

                                llComment.addView(root_answer);
                            }

                            View div = new View(BaoCommentDetailActivity.this);
                            div.setBackgroundColor(getResources().getColor(R.color.colorGrayE7));
                            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
                            lp.topMargin=20;
                            div.setLayoutParams(lp);

                            llComment.addView(div);
                        }

                    }


            } else {
                tvComment.setText("");
                llComment.setVisibility(View.GONE);
            }

    }


    private OnItemInternalClick commentItemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            FComment comment = (FComment) adapter.getItem(position);
            if (!TextUtils.isEmpty(comment.getCommentId())) {
                showCommentDialog(dynamicsItem.getInfoId(), comment.getCommentId(), commentType);
            } else {
                toast("评论id为空");
            }
        }
    };

    OnItemInternalClick internalClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            switch (view.getId()) {
//                case R.id.tv_anchor_up:
//                    if (!UserManager.isLoginS()) {
//                        toLogin();
//                        return;
//                    }
//                    doUp();
//                    break;
                case R.id.item_anchor_home_dynamics_video_play:
                    ADynamicsItem dynamics = (ADynamicsItem) adapter.getItem(position);
                    String contentVideoUrl = UrlUtils.linkUrls(Constances.ST_BASE_URL, dynamics.getContentVideo());
                    SkipUtil.skipToVideoPlay(BaoCommentDetailActivity.this, contentVideoUrl, dynamics.getPlayedPos(), position);
                    break;
            }
        }
    };

    private AnchorHomeDynamicsHolder lastHolder = null;
    private void doUp() {
        tvUp.setDrawableEnlarge();

        ParamsEditor pe = new ParamsEditor();
        pe.put("dynamicId", dynamicsItem.getInfoId());
        pe.put("userId", UserManager.getInstance().getUser().getUserId());
        pe.put("fType", 2);


        NetUtils.getNetAdapter().doUpForDynamics(getOwnerName(), pe.getEncryptedParams(UserManager.getInstance().getToken()), new AbsNetCallBack<SingleResult>(SingleResult.class) {
            @Override
            public void onSuc(SingleResult singleResult) {
                if (singleResult.isSuccess()) {
                    if (null != dynamicsItem) {
                        if (dynamicsItem.getIsDigg()) {
                            dynamicsItem.setIsDigg(!dynamicsItem.getIsDigg());
                            dynamicsItem.setCountDigg(dynamicsItem.getCountDigg() - 1);
                            EventBus.getDefault().post(new DoUpEvent(position,dynamicsItem.getIsDigg(),dynamicsItem.getCountDigg()));

                        } else {
                            dynamicsItem.setIsDigg(!dynamicsItem.getIsDigg());
                            dynamicsItem.setCountDigg(dynamicsItem.getCountDigg() + 1);
                            EventBus.getDefault().post(new DoUpEvent(position,dynamicsItem.getIsDigg(),dynamicsItem.getCountDigg()));
                        }

                        if (dynamicsItem.getIsDigg()) {
                            Drawable drawable = BaoCommentDetailActivity.this.getResources().getDrawable(R.mipmap.icon_compere_btn_press);
                            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                            tvUp.setDrawables(drawable, null, null, null);
                            tvUp.setTextColor(BaoCommentDetailActivity.this.getResources().getColor(R.color.colorTxRedMain_));
                        } else {
                            Drawable drawable = BaoCommentDetailActivity.this.getResources().getDrawable(R.mipmap.icon_compere_btn_normal);
                            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                            tvUp.setDrawables(drawable, null, null, null);
                            tvUp.setTextColor(BaoCommentDetailActivity.this.getResources().getColor(R.color.colorOpinionSelectNo));
                        }
                        tvUp.setText("" + dynamicsItem.getCountDigg());
                        if (dynamicsItem.getIsDigg()) {
                            addInvitation(Constances.ADD_INTEGRAL_DOUP, false);
                        }
                    }

                }

                }

        });




    }


    public void initTop(FRebellion aDynamicsItem) {
        name.setText(!TextUtils.isEmpty(aDynamicsItem.getUserName()) ? aDynamicsItem.getUserName() : "");
        time.setText(!TextUtils.isEmpty(aDynamicsItem.getPubTime()) ? DateUtils.timeAgo(DateUtils.getDataDate(aDynamicsItem)) : "");
        intro.setTextHtml("<font color=\"#F75C43\">" + (TextUtils.isEmpty(aDynamicsItem.getTopic()) ? "" : aDynamicsItem.getTopic()) + "</font>"+
                "<font color=\"#222222\">" +(TextUtils.isEmpty(aDynamicsItem.getDiscloseText()) ? "" : aDynamicsItem.getDiscloseText()+ "</font>" ),true);
//        intro.setText(!TextUtils.isEmpty(aDynamicsItem.getDiscloseText()) ? aDynamicsItem.getDiscloseText() : "");
        address_cv.setText(aDynamicsItem.getLocation());

        if(!TextUtils.isEmpty(aDynamicsItem.getBaoType()) && aDynamicsItem.getBaoType().equals("1")){
            if(!TextUtils.isEmpty(aDynamicsItem.getNote())){
                bao_title.setText(aDynamicsItem.getNote());
                bao_title.setVisibility(View.VISIBLE);
                bao_title.setBackgroundColor(getResources().getColor(R.color.comment_type_warning_bg));
            }else{
                bao_title.setVisibility(View.GONE);
            }
        }else if(!TextUtils.isEmpty(aDynamicsItem.getBaoType()) && aDynamicsItem.getBaoType().equals("0")){
            if(!TextUtils.isEmpty(aDynamicsItem.getNote())){
                bao_title.setText(aDynamicsItem.getNote());
                bao_title.setVisibility(View.VISIBLE);
                bao_title.setBackgroundColor(getResources().getColor(R.color.comment_type_bg));
            }else{
                bao_title.setVisibility(View.GONE);
            }
        }else {
            bao_title.setVisibility(View.GONE);
        }


            int count = aDynamicsItem.getCountComment();
            if (count > 0) {
                tvComment.setText(String.valueOf(count));
            } else {
                tvComment.setText("");
            }

        if (!aDynamicsItem.getIsDigg()) {
            Drawable drawable = getResources().getDrawable(R.mipmap.icon_compere_btn_normal);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tvUp.setDrawables(drawable, null, null, null);
            tvUp.setTextColor(getResources().getColor(R.color.colorOpinionSelectNo));
        } else {
            Drawable drawable = getResources().getDrawable(R.mipmap.icon_compere_btn_press);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tvUp.setDrawables(drawable, null, null, null);
            tvUp.setTextColor(getResources().getColor(R.color.colorRedMain));
        }
        int upCount = aDynamicsItem.getCountDigg();
        if (upCount > 0) {
            tvUp.setText(String.valueOf(upCount));
        } else {
            tvUp.setText("");
        }
        tvUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (internalClick != null) {
                    if (!UserManager.isLoginS()) {
                        toLogin();
                        return;
                    }
                    doUp();
                }
            }
        });
        tvComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (internalClick != null) {
        //            internalClick.onItemInternalClick(itemView, v, getAdapterPosition());
                }
            }
        });

        if (!TextUtils.isEmpty(aDynamicsItem.getUserAvator())) {
            GlideUtil.getGlid(BaoCommentDetailActivity.this, aDynamicsItem.getUserAvator()).error(R.mipmap.icon_circle_avatar)
                    .placeholder(R.mipmap.icon_circle_avatar).transform(new GlideCircleTransform(BaoCommentDetailActivity.this))
                    .into(img);
        } else {
            img.setImageResource(R.mipmap.icon_circle_avatar);
        }
        int pa = getResources().getDimensionPixelOffset(R.dimen.item_list_large_margin_top);
        intro.resetState(aDynamicsItem.isShrink());
        if (TextUtils.isEmpty(aDynamicsItem.getDiscloseVideo()) && !ListUtils.isListValued(aDynamicsItem.getDiscloseImage())) {
            rl_type.setVisibility(View.GONE);
            intro.setPadding(pa, 0, pa, pa);
        } else {
            rl_type.setVisibility(View.VISIBLE);
            intro.setPadding(pa, 0, pa, 0);
        }
        if (TextUtils.isEmpty(aDynamicsItem.getDiscloseVideo()) ) {
            play.setVisibility(View.GONE);
            coverImage.setVisibility(View.GONE);

            if (ListUtils.isListValued(aDynamicsItem.getDiscloseImage())) {
                if (aDynamicsItem.getDiscloseImage().size() == 1) {
                    pic_list.setVisibility(View.GONE);
                    video_rl.setVisibility(View.VISIBLE);
                    coverImage.setVisibility(View.VISIBLE);
                    four_ll.setVisibility(View.GONE);
//                    singleImg.setVisibility(View.VISIBLE);
//                    GlideUtil.getGlideWithSmall169Def(context, contentImage.get(0).getImageUrl()).into(singleImg);
                    GlideUtil.getGlideWith11DefNoFit(BaoCommentDetailActivity.this, aDynamicsItem.getDiscloseImage().get(0).getImageUrl())
//                            .fitCenter()
//                            .listener(lis)
                            .into(coverImage);
                } else if (aDynamicsItem.getDiscloseImage().size() == 4) {
                    pic_list.setVisibility(View.GONE);
                    video_rl.setVisibility(View.GONE);
                    four_ll.setVisibility(View.VISIBLE);
                    GlideUtil.loadImgWith11Def(BaoCommentDetailActivity.this, aDynamicsItem.getDiscloseImage().get(0).getImageUrl(), img1);
                    GlideUtil.loadImgWith11Def(BaoCommentDetailActivity.this, aDynamicsItem.getDiscloseImage().get(1).getImageUrl(), img2);
                    GlideUtil.loadImgWith11Def(BaoCommentDetailActivity.this, aDynamicsItem.getDiscloseImage().get(2).getImageUrl(), img4);
                    GlideUtil.loadImgWith11Def(BaoCommentDetailActivity.this, aDynamicsItem.getDiscloseImage().get(3).getImageUrl(), img5);
                    img3.setImageDrawable(null);
                    img6.setImageDrawable(null);
                } else {
                    initView(aDynamicsItem.getDiscloseImage());
                    pic_list.setVisibility(View.VISIBLE);
                    video_rl.setVisibility(View.GONE);
                    four_ll.setVisibility(View.GONE);
                }
            } else {
                pic_list.setVisibility(View.GONE);
                video_rl.setVisibility(View.GONE);
                four_ll.setVisibility(View.GONE);
//                singleImg.setVisibility(View.GONE);
            }
        } else {
            pic_list.setVisibility(View.GONE);
            four_ll.setVisibility(View.GONE);
//            singleImg.setVisibility(View.GONE);
            video_rl.setVisibility(View.VISIBLE);
            coverImage.setVisibility(View.VISIBLE);
            play.setVisibility(View.VISIBLE);
            String coverImageUrl = aDynamicsItem.getCoverImage();
//            DrawableRequestBuilder<String> drb=
            GlideUtil.getGlideWithMid169DefNoFit(BaoCommentDetailActivity.this, coverImageUrl)
//                            .fitCenter()
//                            .listener(lis)
                    .into(coverImage);

        }
    }

    private void initView(List<ImageUr> dataImgs) {
        manager = new GridLayoutManager(BaoCommentDetailActivity.this, 3);
        pic_list.setItemAnimator(new DefaultItemAnimator());
        pic_list.setLayoutManager(manager);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        pic_list.setHasFixedSize(true);
        pic_list.setNestedScrollingEnabled(false);
        if (picAdapter == null) {
            // new UiListMarginUtils().setMargin(context, pic_list, R.dimen.item_list_large_margin_left, R.dimen.item_list_large_margin_right);
            picAdapter = new PicAdapter(BaoCommentDetailActivity.this, dataImgs);
        } else {
            picAdapter.setData(dataImgs);
        }
        pic_list.setAdapter(picAdapter);
    }




    @OnClick({R.id.item_rebllion_img_single, R.id.item_rebllion_video_img})
    public void onlyImgClick(View view) {
        if (dynamicsItem == null || !TextUtils.isEmpty(dynamicsItem.getDiscloseVideo())
                || dynamicsItem.getDiscloseImage() == null || dynamicsItem.getDiscloseImage().size() == 0
                || dynamicsItem.getDiscloseImage().get(0) == null || TextUtils.isEmpty(dynamicsItem.getDiscloseImage().get(0).getImageUrl())) {
            return;
        }
        String path = UrlUtils.linkUrls(Constances.ST_BASE_URL, dynamicsItem.getDiscloseImage().get(0).getImageUrl());
        String type = (path.endsWith(".gif") || path.endsWith(".GIF")) ? "image/gif" : "image";
        Intent intent = new Intent(BaoCommentDetailActivity.this, PictureExternalPreviewActivity.class);
        intent.putExtra("type", 1);
        ArrayList<LocalMedia> list = new ArrayList<>();
        list.add(new LocalMedia(path, "", type));
        intent.putExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST, list);
        intent.putExtra(PictureConfig.EXTRA_POSITION, 0);
        startActivity(intent);
    }


    @OnClick(value = {R.id.item_rebllion_pic_img1, R.id.item_rebllion_pic_img2,
            R.id.item_rebllion_pic_img4, R.id.item_rebllion_pic_img5})
    public void imgClick(View view) {
        Intent intent = new Intent(BaoCommentDetailActivity.this, PictureExternalPreviewActivity.class);
        switch (view.getId()) {
            case R.id.item_rebllion_pic_img1:
                intent.putExtra(PictureConfig.EXTRA_POSITION, 0);
                break;
            case R.id.item_rebllion_pic_img2:
                intent.putExtra(PictureConfig.EXTRA_POSITION, 1);
                break;
            case R.id.item_rebllion_pic_img4:
                intent.putExtra(PictureConfig.EXTRA_POSITION, 2);
                break;
            case R.id.item_rebllion_pic_img5:
                intent.putExtra(PictureConfig.EXTRA_POSITION, 3);
                break;

        }
        List<LocalMedia> images = new ArrayList<LocalMedia>();
        for (ImageUr imageUr : dynamicsItem.getDiscloseImage()) {
            LocalMedia localMedia = new LocalMedia();
            localMedia.setPath(Constances.ST_BASE_URL + imageUr.getImageUrl());
            images.add(localMedia);
        }

        intent.putExtra("type", 1);
        intent.putExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST, (Serializable) images);
        startActivity(intent);
    }


}
